// pages/handle/set.js
var app = getApp();
var utils = require('../../../utils/util.js');
var common = require('../../../service/common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filePath: '',
    page: 1,
    searchList: [],
    hideSearchBox: true,
    hideMobileBox: false,
    shortName: '',
    companyName: '',
    mobile: '',
    address: '',
    resId: 0,
    logo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    _this.setData({
        resId: options.id
    });
    _this.getCompanyDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      var _this = this;
      _this.getCompanyDetail(1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  // 获取公司信息
  getCompanyDetail: function (isPullDown) {
    var _this = this,
        id = _this.data.resId,
        reqJson = {
            token: utils.getToken(app),
            website_id: id
        },
        sucFuc = function (res) {
            if (res.data.data) {
                _this.setData({
                    shortName: res.data.data.company_short_name,
                    companyName: res.data.data.company_name,
                    mobile: res.data.data.mobile,
                    address: res.data.data.address,
                    filePath: res.data.data.logo,
                    logo: res.data.data.logo
                });
            }
            if (isPullDown == 1) {
                wx.stopPullDownRefresh();
            }
        };
    // ajax提交
    common.getAjaxData(common.getUrl('website.website'), reqJson, sucFuc);
  },

  // 上传logo
  uploadLogo: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '保存中',
          mask: true
        })
        var filePath = res.tempFilePaths[0];
        that.setData({
          filePath: filePath
        })
        wx.hideLoading();
        that.uploadImage(filePath);
      }
    })
  },

  // 更新头像
  uploadImage: function (tempFile) {
    utils.loading('logo提交中');
    var _this = this;
    wx.uploadFile({
      url: common.getUrl('website.uploadLogo'),
      filePath: tempFile,
      name: 'file',
      header: {
        'content-type': 'multipart/form-data;'
      },
      formData: { token: utils.getToken(app) },
      success: function (res) {
        utils.hideLoad();
      },
      fail: function (res) {
        utils.hideLoad();
        // 失败提示框
        utils.tipBox({
          title: '结果',
          content: res.data.msg,
          showCancel: false
        });
      },
      complete: function (res) {
        utils.hideLoad();
        var data = JSON.parse(res.data);
        _this.setData({
            logo: data.data.url
        })
      }
    })
  },

  // 模糊搜索公司名称
  searchCompany: function (e) {
    var _this = this,
      searchTxt = e.detail.value,
      page = _this.data.page,
      reqJson = {
        token: utils.getToken(app),
        page: _this.data.page,
        name: searchTxt,
        type: 2,
        isLoading: 1
      },
      sucFuc = function (res) {
        console.log(res);
        if (res.data.data.length > 0) {
            _this.setData({
                searchList: res.data.data,
                page: page + 1,
                hideSearchBox: false,
                hideMobileBox: true
            });
        } else {
            _this.setData({
                searchList: [],
                page: 1,
                hideSearchBox: true,
                hideMobileBox: false
            });
        }
      };
    // ajax提交
    if (searchTxt != '') {
      common.getAjaxData(common.getUrl('website.searchBy'), reqJson, sucFuc);
    } else {
      _this.hideSearchBox();
    }
  },

  // 隐藏搜索列表
  hideSearchBox: function () {
    var _this = this;
    _this.setData({
      hideSearchBox: true,
      hideMobileBox: false,
      searchList: []
    })
  },

  // 创建官网
  upWebsite: function (e) {
    var _this = this,
      sucFuc = '',
      shortName = e.detail.value.short_name,
      companyName = e.detail.value.name,
      mobile = e.detail.value.mobile,
      logo = _this.data.logo,
      address = e.detail.value.address,
      website_id = _this.data.resId,
      sendData = {},
      data = {};
    // 检测内容是否为空
    if (utils.checkIsNull(shortName, '公司简称不能为空') == false) {
      return false;
    }
    if (utils.checkIsNull(companyName, '公司名称不能为空') == false) {
      return false;
    }
    if (utils.checkIsNull(mobile, '联系电话不能为空') == false) {
      return false;
    }
    if (!/^1[34578]\d{9}$/.test(mobile)) {
      utils.tipBox({ title: '提示', content: '请输入正确的手机号码', cancel: false });
      return false;
    }
    // 拼接传输数据
    sendData.short_name = shortName;
    sendData.name = companyName;
    sendData.mobile = mobile;
    sendData.logo = logo;
    sendData.address = address;
    sendData.website_id = website_id;
    sendData.token = utils.getToken(app);
    sucFuc = function (res) {
        if (res.data.code == 0) {
            utils.toast({ title: '设置官网成功！' });
            setTimeout(function () {
                wx.switchTab({
                    url: '/pages/manage/index'
                })
            }, 1000);
        } else {
            utils.tipBox({ title: '提示', content: res.data.message, cancel: false });
        }      
    };
    // ajax提交
    common.getAjaxData(common.getUrl('website.saveWebsite'), sendData, sucFuc);
  },

  // 点击搜索内容
  clickTap: function (e) {
    var _this = this, clickTxt = e.currentTarget.dataset.name;
    _this.setData({
      companyName: clickTxt,
      hideSearchBox: true,
      hideMobileBox: false,
      searchList: []
    })
  },

  // 删除官网
  delNetwork: function () {
    var _this = this, clickFuc = '';
    clickFuc = function(ret) {
        var reqJson = {
                token: utils.getToken(app),
                website_id: _this.data.resId
            },
            sucFuc = function (res) {
                console.log(res);
                if (res.data.code == 0) {
                    utils.toast({ title: '删除官网成功！' });
                    setTimeout(function () {
                        wx.switchTab({
                            url: '/pages/manage/index'
                        })
                    }, 1000);
                } else {
                    utils.tipBox({ title: '提示', content: '服务器正忙，请稍后再试！', cancel: false });
                }    
            };
        // ajax提交
        common.getAjaxData(common.getUrl('website.delWebsite'), reqJson, sucFuc);
    }
    utils.tipBox({ content: '您确定要删除该官网吗？', sucFuc: clickFuc });
  },

  //选择当前的地址
  selectAddress: function(){
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.chooseLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28,
          success: function(res){
            that.setData({
              address: res.address
            })    
          }
        })
      }
    })
  }
})